#!/bin/bash

channel_server='172.16.3.171'
cache_server='172.16.3.181'
location_server='172.16.3.191'
friends_server='172.16.3.201'
trunk_path='/data/Service/projectz/trunk'

pass="rpaqlf"

ssh_channel="sshpass -p $pass ssh -oStrictHostKeyChecking=no gamevil@$channel_server"
ssh_cache="sshpass -p $pass ssh -oStrictHostKeyChecking=no gamevil@$cache_server"
ssh_location="sshpass -p $pass ssh -oStrictHostKeyChecking=no gamevil@$location_server"
ssh_friends="sshpass -p $pass ssh -oStrictHostKeyChecking=no gamevil@$friends_server"

channel_build()
{
$ssh_channel \
"
svn up
cd $trunk_path/shell
chmod 755 *.sh
./kill.sh
./make.sh
"
}

cache_build()
{
$ssh_cache \
"
svn up
cd $trunk_path/shell
chmod 755 *.sh
./kill.sh
./make.sh
"
}

location_build()
{
$ssh_location \
"
svn up
cd $trunk_path/shell
chmod 755 *.sh
./kill.sh
./make.sh
"
}

friends_build()
{
$ssh_friends \
"
svn up
cd $trunk_path/shell
chmod 755 *.sh
./kill.sh
./make.sh
"
}

channel_restart()
{
$ssh_channel \
"
$trunk_path/shell/kill.sh
cd $trunk_path/shell
nohup ./observer.sh 1 > /dev/null 2>&1 &
"
}

cache_restart()
{
$ssh_cache \
"
$trunk_path/shell/kill.sh
cd $trunk_path/shell
nohup ./observer.sh 2 > /dev/null 2>&1 &
"
}

location_restart()
{
$ssh_location \
"
$trunk_path/shell/kill.sh
cd $trunk_path/shell
nohup ./observer.sh 3 > /dev/null 2>&1 &
"
}

friends_restart()
{
$ssh_friends \
"
$trunk_path/shell/kill.sh
cd $trunk_path/shell
nohup ./observer.sh 4 > /dev/null 2>&1 &
"
}

all_restart()
{
	friends_restart
	location_restart
	cache_restart
	channel_restart
}

channel_kill()
{
$ssh_channel \
"
$trunk_path/shell/kill.sh
"
}

cache_kill()
{
$ssh_cache \
"
$trunk_path/shell/kill.sh
"
}

location_kill()
{
$ssh_location \
"
$trunk_path/shell/kill.sh
"
}

friends_kill()
{
$ssh_friends \
"
$trunk_path/shell/kill.sh
"
}

all_kill()
{
	channel_kill
	cache_kill
	location_kill
	friends_kill
}

channel_show()
{
echo "channel server"
$ssh_channel \
"
$trunk_path/shell/show.sh
"
}

cache_show()
{
echo "cache server"
$ssh_cache \
"
$trunk_path/shell/show.sh
"
}

location_show()
{
echo "location server"
$ssh_location \
"
$trunk_path/shell/show.sh
"
}

friends_show()
{
echo "friend server"
$ssh_friends \
"
$trunk_path/shell/show.sh
"
}

all_show()
{
	channel_show
	cache_show
	location_show
	friends_show
}

middleserver_kill()
{
	cache_kill
	location_kill
	friends_kill
}

middleserver_start()
{
	friends_restart
	location_restart
	cache_restart
}

figlet P h o n e  B u i l d
echo "1: channel build and start"
echo "2: cache build and start"
echo "3: location build and start"
echo "4: friends build and start"
echo "5: all : 4 -> 3 -> 2 -> 1 build and start"
echo "6: channel_restart"
echo "7: all restart"
echo "8: all kill"
echo "9: all show"
echo "10: channel kill"
echo "11: channel start"
echo "12: middle server kill"
echo "13: middle server start"

echo ""
echo -n "choice : "
read input

case $input in
	1) channel_build ;;
	2) cache_build ;;
	3) location_build ;;
	4) friends_build ;;
	5)  friends_build
		location_build 
		cache_build
		channel_build ;;
	6) channel_restart ;;
	7) all_restart ;;
	8) all_kill ;;
	9) all_show ;;
	10)channel_kill ;;
	11)channel_restart ;;
	12)middleserver_kill ;;
	13)middleserver_start ;;

	*) echo "not found command"
esac


